package com.jiyun.controller;


import com.jiyun.pojo.Result;
import com.jiyun.pojo.Shoppingcart;
import com.jiyun.service.IShoppingcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
import java.util.UUID;


/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-19
 */
@RestController
@RequestMapping("/api/shoppingcart")
public class ShoppingcartController {

    @Autowired
    private IShoppingcartService shoppingcartService;




    @PostMapping("shoppingcartshow")
    public Result shoppingcartshow(){
        List<Shoppingcart> list = shoppingcartService.list();
        return Result.success(list);
    }

    @PostMapping("shoppingcartadd")
    public void shoppingcartadd(@RequestBody List<Shoppingcart> shoppingcart,@RequestParam Integer id){
        for (Shoppingcart cart : shoppingcart) {
            cart.setId(generateTenDigitRandomNumber());
            cart.setUid(id);
            shoppingcartService.save(cart);
        }
    }


    @PostMapping("shoppingcartdelete")
    public void  shoppingcartdelete(@RequestParam String [] id){
        for (String i : id) {
            shoppingcartService.removeById(i);
        }
    }


    @PostMapping("shoppingcartselectbyid")
    public Result shoppingcartselectbyid(@RequestParam String[] id){
        List<Shoppingcart> shoppingcart = shoppingcartService.shoppingcartselectbyid(id);
        return Result.success(shoppingcart);
    }

    // 生成10位纯数字随机数
    public String generateTenDigitRandomNumber() {
        Random random = new Random();
        StringBuilder randomNumber = new StringBuilder(10);

        // 确保首位非零
        randomNumber.append(random.nextInt(9) + 1); // 1-9之间的随机整数

        // 生成接下来的9位数字
        for (int i = 1; i < 10; i++) {
            randomNumber.append(random.nextInt(10)); // 0-9之间的随机整数
        }
        return randomNumber.toString();
    }










}
